﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Autodesk.Revit.UI;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YArchitech.MEP.Electric.Common;

namespace Model
{
	public partial class DlgCableTrayBatchCode : System.Windows.Forms.Form
	{
		public DlgCableTrayBatchCode(UIDocument m_uiDoc)
		{
			this.InitializeComponent();
			this.uiDoc = m_uiDoc;
			this.nudSequence.TextChanged += this.nudSequence_TextChanged;
		}

		private void DlgCableTrayCode_Load(object sender, EventArgs e)
		{
			base.Icon = Resource.YArchitechIcon;
			this.xmlHelper.LoadXml(this.path, "Root");
			this.codeInfo[0] = "CT";
			this.codeInfo[1] = "?";
			this.codeInfo[2] = "?";
			this.codeInfo[3] = "1";
			this.LoadDlgData();
		}

		public void SaveDlgData()
		{
			this.xmlHelper.SelectNode(Path.Combine("Root", "HYCableTrayCode"));
			this.xmlHelper.SelectNode(Path.Combine("Root", "HYCableTrayCode", "Params"));
			this.xmlHelper.SetAttribute("CableTrayCodeValue", this.txtCableTrayCode.Text);
			this.xmlHelper.SetAttribute("Mark", this.chkMark.Checked.ToString());
			this.xmlHelper.SetAttribute("MarkValue", this.txtMark.Text);
			this.xmlHelper.SetAttribute("Level", this.chkLevel.Checked.ToString());
			this.xmlHelper.SetAttribute("Direction", this.chkDirection.Checked.ToString());
			this.xmlHelper.SetAttribute("DigitalCode", this.chkDigitalCode.Checked.ToString());
			this.xmlHelper.SetAttribute("DigitalCodeValue", this.nudSequence.Value.ToString());
			if (this.rdoBatch.Checked)
			{
				this.xmlHelper.SetAttribute("Option", "0");
			}
			else if (this.rdoConnection.Checked)
			{
				this.xmlHelper.SetAttribute("Option", "1");
			}
			else
			{
				this.xmlHelper.SetAttribute("Option", "2");
			}
			this.xmlHelper.SaveXml("");
		}

		public void LoadDlgData()
		{
			this.xmlHelper.SelectNode(Path.Combine("Root", "HYCableTrayCode", "Params"));
			string attribute = this.xmlHelper.GetAttribute("CableTrayCodeValue");
			if (!string.IsNullOrEmpty(attribute))
			{
				this.txtCableTrayCode.Text = attribute;
			}
			string attribute2 = this.xmlHelper.GetAttribute("Mark");
			if (!string.IsNullOrEmpty(attribute2))
			{
				this.chkMark.Checked = Convert.ToBoolean(attribute2);
			}
			string attribute3 = this.xmlHelper.GetAttribute("MarkValue");
			if (!string.IsNullOrEmpty(attribute3))
			{
				this.txtMark.Text = attribute3;
			}
			string attribute4 = this.xmlHelper.GetAttribute("Level");
			if (!string.IsNullOrEmpty(attribute4))
			{
				this.chkLevel.Checked = Convert.ToBoolean(attribute4);
			}
			string attribute5 = this.xmlHelper.GetAttribute("Direction");
			if (!string.IsNullOrEmpty(attribute5))
			{
				this.chkDirection.Checked = Convert.ToBoolean(attribute5);
			}
			string attribute6 = this.xmlHelper.GetAttribute("DigitalCode");
			if (!string.IsNullOrEmpty(attribute6))
			{
				this.chkDigitalCode.Checked = Convert.ToBoolean(attribute6);
			}
			string attribute7 = this.xmlHelper.GetAttribute("DigitalCodeValue");
			if (!string.IsNullOrEmpty(attribute7))
			{
				this.nudSequence.Value = Convert.ToInt32(attribute7);
			}
			string attribute8 = this.xmlHelper.GetAttribute("Option");
			if (!string.IsNullOrEmpty("Option"))
			{
				if (attribute8 == "0")
				{
					this.rdoBatch.Checked = true;
					return;
				}
				if (attribute8 == "1")
				{
					this.rdoConnection.Checked = true;
					return;
				}
				this.rdoAll.Checked = true;
			}
		}

		private void chkMark_CheckedChanged(object sender, EventArgs e)
		{
			if (this.chkMark.Checked)
			{
				this.txtMark.Enabled = true;
				this.codeInfo[0] = this.txtMark.Text;
			}
			else
			{
				this.txtMark.Enabled = false;
				this.codeInfo[0] = "";
			}
			string trayCodeValue = this.GetTrayCodeValue();
			if (!string.IsNullOrEmpty(trayCodeValue))
			{
				this.txtCableTrayCode.Text = trayCodeValue;
				return;
			}
			this.CodeCompoundModeSet(DlgCableTrayBatchCode.CodeCompoundMode.Mark);
		}

		private void chkLevel_CheckedChanged(object sender, EventArgs e)
		{
			if (this.chkLevel.Checked)
			{
				this.codeInfo[1] = "?";
			}
			else
			{
				this.codeInfo[1] = "";
			}
			string trayCodeValue = this.GetTrayCodeValue();
			if (!string.IsNullOrEmpty(trayCodeValue))
			{
				this.txtCableTrayCode.Text = trayCodeValue;
				return;
			}
			this.CodeCompoundModeSet(DlgCableTrayBatchCode.CodeCompoundMode.Level);
		}

		private void chkDirection_CheckedChanged(object sender, EventArgs e)
		{
			if (this.chkDirection.Checked)
			{
				this.codeInfo[2] = "?";
			}
			else
			{
				this.codeInfo[2] = "";
			}
			string trayCodeValue = this.GetTrayCodeValue();
			if (!string.IsNullOrEmpty(trayCodeValue))
			{
				this.txtCableTrayCode.Text = trayCodeValue;
				return;
			}
			this.CodeCompoundModeSet(DlgCableTrayBatchCode.CodeCompoundMode.Direction);
		}

		private void chkDigitalCode_CheckedChanged(object sender, EventArgs e)
		{
			if (this.chkDigitalCode.Checked)
			{
				this.nudSequence.Enabled = true;
				this.codeInfo[3] = this.nudSequence.Value.ToString();
			}
			else
			{
				this.nudSequence.Enabled = false;
				this.codeInfo[3] = "";
			}
			string trayCodeValue = this.GetTrayCodeValue();
			if (!string.IsNullOrEmpty(trayCodeValue))
			{
				this.txtCableTrayCode.Text = trayCodeValue;
				return;
			}
			this.CodeCompoundModeSet(DlgCableTrayBatchCode.CodeCompoundMode.DigitalCode);
		}

		private string GetTrayCodeValue()
		{
			string text = "";
			foreach (string text2 in this.codeInfo)
			{
				if (!string.IsNullOrEmpty(text2))
				{
					text = text + text2 + "-";
				}
			}
			if (text.Length > 0)
			{
				text = text.Remove(text.Length - 1, 1);
			}
			else
			{
				TaskDialog.Show("盈建科提示", "桥架编码不能为空,至少选择一项。");
			}
			return text;
		}

		private void CodeCompoundModeSet(DlgCableTrayBatchCode.CodeCompoundMode mode)
		{
			if (mode == DlgCableTrayBatchCode.CodeCompoundMode.Mark)
			{
				this.chkMark.Checked = true;
				this.txtMark.Enabled = true;
				return;
			}
			if (mode == DlgCableTrayBatchCode.CodeCompoundMode.Level)
			{
				this.chkLevel.Checked = true;
				return;
			}
			if (mode == DlgCableTrayBatchCode.CodeCompoundMode.Direction)
			{
				this.chkDirection.Checked = true;
				return;
			}
			if (mode == DlgCableTrayBatchCode.CodeCompoundMode.DigitalCode)
			{
				this.chkDigitalCode.Checked = true;
				this.nudSequence.Enabled = true;
			}
		}

		private void btnDirection_Click(object sender, EventArgs e)
		{
			TaskDialog.Show("桥架方向说明", "桥架方向：X:横向；Y:纵向；Z:竖向；T:其他方向。");
		}

		private void txtMark_TextChanged(object sender, EventArgs e)
		{
			if (this.txtMark.Text.Length == 0)
			{
				TaskDialog.Show("盈建科提示", "参数不能为空。");
				this.txtMark.Text = "CT";
				this.txtMark.SelectAll();
			}
			this.codeInfo[0] = this.txtMark.Text;
			this.txtCableTrayCode.Text = this.GetTrayCodeValue();
		}

		private void nudSequence_TextChanged(object sender, EventArgs e)
		{
			if (string.IsNullOrEmpty(this.nudSequence.Text))
			{
				this.nudSequence.Text = "1";
			}
			this.codeInfo[3] = this.nudSequence.Value.ToString();
			this.txtCableTrayCode.Text = this.GetTrayCodeValue();
		}

		private void DlgCableTrayBatchCode_FormClosed(object sender, FormClosedEventArgs e)
		{
			this.SaveDlgData();
		}

		private UIDocument uiDoc;

		private string[] codeInfo = new string[4];

		private YArchitech.MEP.Electric.Common.XmlHelper xmlHelper = new YArchitech.MEP.Electric.Common.XmlHelper();

		private string path = Path.Combine(Product.WorkingLocation, "CableTrayBatchCode.xml");

		private const string Root = "Root";

		private const string Node = "HYCableTrayCode";

		private const string ChildNode = "Params";

		private const string CableTrayCodeValue = "CableTrayCodeValue";

		private const string Mark = "Mark";

		private const string MarkValue = "MarkValue";

		private const string Level = "Level";

		private const string Direction = "Direction";

		private const string DigitalCode = "DigitalCode";

		private const string DigitalCodeValue = "DigitalCodeValue";

		private const string Option = "Option";

		private enum CodeCompoundMode
		{
			Mark,
			Direction,
			Level,
			DigitalCode
		}
	}
}
